package lwf.db.manager;

import java.util.ArrayList;
import java.util.List;

import net.lwf.factory.HangmanData;
import net.lwf.hibernate.entity.Hangman;
import net.lwf.number.util.RandomNumberGenerator;


public class HangmanManager {
	
	private static final int DATA_ROWS = 4;
	
	public String getData() {
		List<Hangman> data = this.prepareData();
		return this.processData(data);
	}
	
	
	private List<Hangman> prepareData() {
		List<Hangman> data = new ArrayList<Hangman>(2*DATA_ROWS);
		List<Hangman> words = HangmanData.getInstance().getWords();
		List<Hangman> quotes = HangmanData.getInstance().getQuotes();
		//List<Hangman> words = this.getTestWords();
		//List<Hangman> quotes = this.getTestQuotes();
		if(words.size() <= DATA_ROWS) {
			data.addAll(words);
		}
		else {
			int[] randomdata = RandomNumberGenerator.generateInts(1, words.size(), DATA_ROWS);
			for(int i=0; i<randomdata.length; i++) {
				data.add(words.get(randomdata[i]-1));
			}
		}
		
		if(quotes.size() <= DATA_ROWS) {
			data.addAll(quotes);
		}
		else {
			int[] randomdata = RandomNumberGenerator.generateInts(1, quotes.size(), DATA_ROWS);
			for(int i=0; i<randomdata.length; i++) {
				data.add(quotes.get(randomdata[i]-1));
			}
		}
		return data;
		
	}
	
	private String processData(List<Hangman> data) {
		//hint1*word1,hint2*word2,hint3*word3
		StringBuilder sbWords = new StringBuilder();
		//hint1*quote1,hint2*quote2,hint3*quote3
		StringBuilder sbQuotes = new StringBuilder();
		for(Hangman record: data) {
			if(record.getType().equalsIgnoreCase("word")) {
				sbWords.append(record.getHint()); 
				sbWords.append("*");
				sbWords.append(record.getAnswer());
				sbWords.append(",");
			}
			if(record.getType().equalsIgnoreCase("quote")) {
				sbQuotes.append(record.getHint()); 
				sbQuotes.append("*");
				sbQuotes.append(record.getAnswer());
				sbQuotes.append(",");
			}
		}

		return trimLastLetter(sbWords) + "|" + trimLastLetter(sbQuotes);
	}


	private String trimLastLetter(StringBuilder sbData) {
		String worddata = sbData.toString();
		worddata = worddata.substring(0, worddata.length()-1);
		return worddata;
	}
	
/*	private List<Hangman> getTestWords() {
		List<Hangman> words = new ArrayList<Hangman>();
		Hangman man = new Hangman();
		man.setHint("Hint one");
		man.setAnswer("Word one");
		man.setType("word");
		words.add(man);
		 man = new Hangman();
			man.setHint("Hint two");
			man.setAnswer("Word two");
			man.setType("word");
			words.add(man);
		 man = new Hangman();
			man.setHint("Hint three");
			man.setAnswer("Word three");
			man.setType("word");
			words.add(man);	
		man = new Hangman();
			man.setHint("Hint four");
			man.setAnswer("Word four");
			man.setType("word");
			words.add(man);
		 man = new Hangman();
			man.setHint("Hint five");
			man.setAnswer("Word five");
			man.setType("word");
			words.add(man);
		man = new Hangman();
			man.setHint("Hint six");
			man.setAnswer("Word six");
			man.setType("word");
			words.add(man);
		man = new Hangman();
			man.setHint("Hint seven");
			man.setAnswer("Word seven");
			man.setType("word");
			words.add(man);
		man = new Hangman();
			man.setHint("Hint eight");
			man.setAnswer("Word eight");
			man.setType("word");
			words.add(man);			
		
			return words;
		
	}
	
	private List<Hangman> getTestQuotes() {
		List<Hangman> quotes = new ArrayList<Hangman>();
		Hangman man = new Hangman();
		man.setHint("Hint one");
		man.setAnswer("Quote one");
		man.setType("quote");
		quotes.add(man);
		 man = new Hangman();
			man.setHint("Hint two");
			man.setAnswer("Quote two");
			man.setType("quote");
			quotes.add(man);
		 man = new Hangman();
			man.setHint("Hint three");
			man.setAnswer("Quote three");
			man.setType("quote");
			quotes.add(man);	
		man = new Hangman();
			man.setHint("Hint four");
			man.setAnswer("Quote four");
			man.setType("quote");
			quotes.add(man);
		 man = new Hangman();
			man.setHint("Hint five");
			man.setAnswer("Quote five");
			man.setType("quote");
			quotes.add(man);
		man = new Hangman();
			man.setHint("Hint six");
			man.setAnswer("Quote six");
			man.setType("quote");
			quotes.add(man);
		man = new Hangman();
			man.setHint("Hint seven");
			man.setAnswer("Quote seven");
			man.setType("quote");
			quotes.add(man);
		man = new Hangman();
			man.setHint("Hint eight");
			man.setAnswer("Quote eight");
			man.setType("quote");
			quotes.add(man);			
		
			return quotes;
		
	}
	
	private void print(int[] randomdata) {
		for(int i=0; i<randomdata.length; i++) {
			System.out.println(".....> Random Index is: " + randomdata[i]);
		}
	}
	
	public static void main(String[] args) {
		HangmanManager manager = new HangmanManager();
		manager.getData();

	}*/


}
